#include<bits/stdc++.h>
#define int long long 
#define pb push_back
#define rep_(i, a, b) for (int i = (a); i <= (b); i++)
using namespace std;
const int maxn = 3e5 + 10;
int n, m;
int a[maxn], c[maxn], b[maxn], h[maxn];
int d[20][505];
int x[5];
int gt(int x) {
	int t = __lg(x);
	if ((1<<t) < x) t++;
	return t;
}
int cur = 0;
vector<int> ck(int ok, vector<int> x, vector<int> y, int R) {
	// cout << "Round" << R << endl;
	// cout << ok << ' ' << h[4]<< endl;
	// for (auto i : x) cout << i << ' '; cout << endl;
	// for (auto i : y) cout << i << ' '; cout << endl;
	// cout<<endl;
	vector<int> tx, ty;
	if (ok) {
		int wn = 0;
		for (auto i : y) {
			if (h[i] >= R) ty.pb(i);
			else wn = 1;
			if (h[i] == -1) ty.pb(i), wn = 1;
		}
		if (wn) tx = x;
		else tx.clear();
	} else {
		int wn = 0;
		for (auto i : x) {
			if (h[i] >= R) tx.pb(i);
			else wn = 1;
			if (h[i] == -1) tx.pb(i), wn = 1;
		}
		if (wn) ty = y;
		else ty.clear();
	}
	vector<int> tmp;
	for (auto i : tx) tmp.pb(i);
	for (auto i : ty) tmp.pb(i);
	return tmp;
}
int qr(vector<vector<int> > v, int R, int G) {
	if (G == 1) {
		int tmp = 0;
		for (auto i : v[0]) tmp += i;
		return tmp;
	}
	vector<vector<int> > nw;
	for (int i = 0, j = 0; i < G; i+=2, j++) {
		nw.pb(ck(d[R][j], v[i], v[i + 1], R));
	}	
	return qr(nw, R + 1, G / 2); 
}
void solve() {
	rep_(i, 0, 3) cin >> x[i];
	rep_(i, 1, n) {
		b[i] = a[i] ^ x[i % 4];
	}
	int ans = 0;
	rep_(i, 1, m) {
		int tmp = 1<<gt(c[i]);
		vector<vector<int> > v;
		rep_(j, 1, c[i]) v.pb({j}), h[j] = b[j];
		rep_(j, c[i] + 1, tmp) v.pb({j}), h[j] = -1;
		int pq = qr(v, 1, tmp); 
		ans = ans ^ (i * pq);
	}
	cout << ans << endl;
}
signed main() {
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	rep_(i, 1, n) cin >> a[i];
	rep_(i, 1, m) cin >> c[i];
	int K = gt(n);
	rep_(i, 1, K) {
		int now = (1<<(K - i));

		rep_(j, 0, now - 1) {
			char x; cin >> x;
			d[i][j] = x - '0';
		}
	}
	int T; cin >> T;
	rep_(i, 1, T) {
		solve(); 
	}
	return 0;
}